//
//  ViewController.m
//  LBLoadingView
//
//  Created by LINAICAI on 2017/1/16.
//  Copyright © 2017年 LINAICAI. All rights reserved.
//

#import "ViewController.h"
#import "LBLoadingView.h"
#import "LBProgressCircleView.h"
#import <libkern/OSAtomic.h>
@interface ViewController ()
@property(nonatomic , strong)UIView *bottomView;
@end

@implementation ViewController
- (void)push{
    ViewController *vc = [[ViewController alloc]initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
- (UIView*)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 300, self.view.bounds.size.width, self.view.bounds.size.height-300)];
        _bottomView.backgroundColor = [UIColor blueColor];
    }
    return _bottomView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Demo";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.view.bounds.size.width/2-300/2, 64 + 10, 300, 30);
    [button setTitle:@"None不阻塞当前界面" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blackColor]];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(self.view.bounds.size.width/2-300/2, 64 + 10 + 40, 300, 30);
    [button1 setTitle:@"Custom阻塞当前界面" forState:UIControlStateNormal];
    [button1 setBackgroundColor:[UIColor blackColor]];
    [button1 addTarget:self action:@selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(self.view.bounds.size.width/2-300/2, 64 + 10 + 40 + 40, 300, 30);
    [button2 setTitle:@"Light阻塞全局界面" forState:UIControlStateNormal];
    [button2 setBackgroundColor:[UIColor blackColor]];
    [button2 addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(self.view.bounds.size.width/2-300/2, 64 + 10 + 40 + 40 + 40, 300, 30);
    [button3 setTitle:@"Black阻塞全局界面" forState:UIControlStateNormal];
    [button3 setBackgroundColor:[UIColor blackColor]];
    [button3 addTarget:self action:@selector(buttonClick3:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(self.view.bounds.size.width/2-300/2, 64 + 10 + 40 + 40 + 40 + 40, 300, 30);
    [button4 setTitle:@"测试" forState:UIControlStateNormal];
    [button4 setBackgroundColor:[UIColor blackColor]];
    [button4 addTarget:self action:@selector(buttonClick4:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    [self.view addSubview:self.bottomView];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)buttonClick4:(id)sender{
    [LBLoadingView setLBLoadingBackgroundCustomColor:[UIColor redColor]];
    [LBLoadingView showInView:self.view style:LBLoadingBackgroundStyleCustom];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
    [LBLoadingView showInView:self.bottomView style:LBLoadingBackgroundStyleBlack];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
}
- (void)buttonClick:(id)sender{
    [LBLoadingView showInView:self.view style:LBLoadingBackgroundStyleNone];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
}
- (void)buttonClick2:(id)sender{
    [LBLoadingView showWithStyle:LBLoadingBackgroundStyleLight];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
}
- (void)buttonClick3:(id)sender{
    [LBLoadingView showWithStyle:LBLoadingBackgroundStyleBlack];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
}
- (void)buttonClick1:(id)sender{
    [LBLoadingView setLBLoadingBackgroundCustomColor:[UIColor redColor]];
    [LBLoadingView showInView:self.view style:LBLoadingBackgroundStyleCustom];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LBLoadingView dismiss];
    });
    return;
    LBProgressCircleView *progressView = [[LBProgressCircleView alloc]initWithFrame:CGRectMake(100, 300, 50, 50)];
    [self.view addSubview:progressView];
    progressView.center = self.view.center;
    
    __block CGFloat timeAllCount=100.0;
    __block int32_t timeOutCount=0;
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        OSAtomicIncrement32(&timeOutCount);
        progressView.progress = timeOutCount/timeAllCount;
        if (timeOutCount == 100) {
            NSLog(@"timersource cancel");
            dispatch_source_cancel(timer);
        }
    });
    
    dispatch_source_set_cancel_handler(timer, ^{
        NSLog(@"timersource cancel handle block");
    });
    
    dispatch_resume(timer);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
